﻿// Copyright © 2012 X-treem Software Limited
// Written by Jason Payne
// Licence: https://wpfgooglesearch.codeplex.com/license

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace Xtreem.GoogleSearch.DataContracts
{
    [DataContract]
    public class GoogleResponseWrapper
    {
        [DataMember]
        public GoogleSearchResults responseData { get; set; }

        [DataMember]
        public string responseDetails { get; set; }

        [DataMember]
        public int responseStatus { get; set; }
    }

    [DataContract(Name = "responseData")]
    public class GoogleSearchResults
    {
        [DataMember(Name = "results", Order = 0)]
        public Result[] results { get; set; }

        [DataMember(Name = "cursor", Order = 1)]
        public Cursor cursor { get; set; }
    }

    [DataContract]
    public class Result
    {
        [DataMember(Order = 0)]
        public string GsearchResultClass { get; set; }

        [DataMember(Order = 1)]
        public string unescapedUrl { get; set; }

        [DataMember(Order = 2)]
        public string url { get; set; }

        [DataMember(Order = 3)]
        public string visibleUrl { get; set; }

        [DataMember(Order = 4)]
        public string cacheUrl { get; set; }

        [DataMember(Order = 5)]
        public string title { get; set; }

        [DataMember(Order = 6)]
        public string titleNoFormatting { get; set; }

        [DataMember(Order = 7)]
        public string content { get; set; }

        public string Html
        {
            get
            {
                Assembly assembly = Assembly.GetExecutingAssembly();
                using (Stream stream = assembly.GetManifestResourceStream("Xtreem.GoogleSearch.Resources.ResultTemplate.htm"))
                using (var reader = new StreamReader(stream))
                {
                    string resultTemplate = reader.ReadToEnd();
                    return string.Format(resultTemplate, url, title, visibleUrl, content);
                }
            }
        }
    }

    [DataContract]
    public class Cursor
    {
        [DataMember(Name = "pages", Order = 0)]
        public Page[] pages { get; set; }

        [DataMember(Order = 1)]
        public string estimatedResultCount { get; set; }

        [DataMember(Order = 2)]
        public int currentPageIndex { get; set; }

        [DataMember(Order = 3)]
        public string moreResultsUrl { get; set; }
    }

    [DataContract]
    public class Page
    {
        [DataMember(Order = 0)]
        public string start { get; set; }

        [DataMember(Order = 1)]
        public string label { get; set; }
    }
}
